package pl.olap.persistence;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;

import pl.olap.dao.Movie;

public class SearchUtil {

	public static List<Movie> searchMovieByTitle(String title) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Movie.class);
		crit.add(Restrictions.ilike("title", "%" + title + "%"));
		List<Movie> mList = crit.list();

		if (mList != null && !mList.isEmpty()) {
			//initialize(mList);
			session.getTransaction().commit();
			session.close();
			return mList;
		}
		session.getTransaction().commit();
		session.close();
		return new ArrayList<Movie>();
	}

	public static List<Movie> searchMovieByActor(String actor) {
		String[] actorArr = actor.split(" ");
		String imie = "";
		String nazwisko = "";
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Movie.class);
		crit.createAlias("persons", "person");

		if (actorArr.length > 1) {
			imie = actorArr[0];
			nazwisko = actorArr[1];
			Junction j = Restrictions.disjunction().add(
					Restrictions.and(
							Restrictions.ilike("person.ID.firstName", "%"
									+ imie + "%"),
							Restrictions.ilike("person.ID.lastName", "%"
									+ nazwisko + "%")));
			j.add(Restrictions.and(
					Restrictions.ilike("person.ID.firstName", "%" + nazwisko
							+ "%"),
					Restrictions.ilike("person.ID.lastName", "%" + imie + "%")));
			crit.add(j);

		} else if (actorArr.length > 0) {
			String n = actorArr[0];
			Junction j = Restrictions.disjunction().add(
					Restrictions.ilike("person.ID.firstName", "%" + n + "%"));
			j.add(Restrictions.ilike("person.ID.lastName", "%" + n + "%"));
			crit.add(j);
		} else {
			return new ArrayList<Movie>();
		}

		List<Movie> mList = crit.list();
		if (mList != null && !mList.isEmpty()) {
			//initialize(mList);
			session.getTransaction().commit();
			session.close();
			return mList;
		}
		session.getTransaction().commit();
		session.close();
		return new ArrayList<Movie>();
	}

	public static List<Movie> searchMovieByProduction(String production) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Movie.class);
		crit.createAlias("productions", "production");

		crit.add(Restrictions.ilike("production.name", "%" + production + "%"));

		List<Movie> mList = crit.list();
		if (mList != null && !mList.isEmpty()) {
			//initialize(mList);
			session.getTransaction().commit();
			session.close();
			return mList;
		}
		session.getTransaction().commit();
		session.close();
		return new ArrayList<Movie>();
	}

	/** Wyszukuje osoby po ocenie w zakresie 1-10 */
	public static List<Movie> searchMovieByRate(String rate) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Movie.class);
		if (isNumeric(rate)) {
			Float r = Float.parseFloat(rate);
			if (r >= 0 && r <= 10) {
				crit.add(Restrictions.between("rating", (r - 1), (r + 1)));
			} else {
				return new ArrayList<Movie>();
			}
		}

		List<Movie> mList = crit.list();
		if (mList != null && !mList.isEmpty()) {
			//initialize(mList);
			session.getTransaction().commit();
			session.close();
			return mList;
		}
		session.getTransaction().commit();
		session.close();
		return new ArrayList<Movie>();
	}

	public static boolean isNumeric(String str) {
		try {
			float d = Float.parseFloat(str);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	public static void initialize(List<Movie> mList) {
		for (Movie m : mList) {
			Hibernate.initialize(m.getPremiere());
			Hibernate.initialize(m.getGenres());
			Hibernate.initialize(m.getPersons());
			Hibernate.initialize(m.getProductions());
		}
	}

	public static void initializeMovie(Movie m) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Hibernate.initialize(m.getPremiere());
		Hibernate.initialize(m.getGenres());
		Hibernate.initialize(m.getPersons());
		Hibernate.initialize(m.getProductions());
		session.getTransaction().commit();
		session.close();
	}

}
